package cn.seapon.talker.bean;

import org.json.JSONObject;

import cn.seapon.talker.common.ChatMsgEntity;
import cn.seapon.talker.common.MessageReceiver;
import cn.seapon.talker.infocarrier.SharedItems;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

/** 
 * seapon_talker
 * @author 作者 E-mail: 
 * @version 创建时间：2015年1月9日 下午4:36:17 
 * 类说明 
 */
@DatabaseTable(tableName = "st_msg")	
public class MessageEntity {

	@DatabaseField(columnName = "id",generatedId=true) 
	private int id;
	@DatabaseField(columnName = "account") 
	private String account;
	@DatabaseField(columnName = "sender")  
	private String sender;
	@DatabaseField(columnName = "receiver")  
	private String receiver;
	@DatabaseField(defaultValue="1",columnName = "type")  
	private int type = 1;
	@DatabaseField(defaultValue="",columnName = "content")  
    private String content;
	@DatabaseField(defaultValue="0",columnName = "posttime")  
    private long posttime;
	//默认为读过的
	@DatabaseField(defaultValue="true",columnName = "hasread")
	private boolean hasRead;
	
	public MessageEntity() {
	}

	public static MessageEntity translateChatMsgEntityToMessageEntity(ChatMsgEntity chatMsgEntity) {
		MessageEntity entity = new MessageEntity();
		entity.setAccount(SharedItems.userInformation.getAccount());
		entity.setContent(chatMsgEntity.getContent());
		entity.setPosttime(chatMsgEntity.getPosttime());
		entity.setReceiver(chatMsgEntity.getReceivers().getTheReceiver());
		entity.setSender(chatMsgEntity.getSenderId());
		entity.setType(chatMsgEntity.getType());
		return entity;
	}
	
	public static ChatMsgEntity rebackMessageEntityToChatMsgEntity(MessageEntity messageEntity) {
		ChatMsgEntity entity = new ChatMsgEntity();
		entity.setContent(messageEntity.getContent());
		entity.setPosttime(messageEntity.getPosttime());
		entity.setReceivers(new MessageReceiver(messageEntity.getReceiver()));
		entity.setSenderId(messageEntity.getSender());
		entity.setType(messageEntity.getType());
		return entity;
	}


	@Override
	public String toString() {
		JSONObject jsonObject = new JSONObject();
		try {
			jsonObject.put("id", id);
			jsonObject.put("sender", sender);
			jsonObject.put("account", account);
			jsonObject.put("receiver", receiver);
			jsonObject.put("type", type);
			jsonObject.put("content", content);
			jsonObject.put("posttime", posttime);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return jsonObject.toString();
	}



	public String getAccount() {
		return account;
	}



	public void setAccount(String account) {
		this.account = account;
	}



	public String getReceiver() {
		return receiver;
	}

	public void setReceiver(String receiver) {
		this.receiver = receiver;
	}

	public int getType() {
		return type;
	}
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getSender() {
		return sender;
	}

	public void setSender(String sender) {
		this.sender = sender;
	}
	public void setType(int type) {
		this.type = type;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public long getPosttime() {
		return posttime;
	}

	public void setPosttime(long posttime) {
		this.posttime = posttime;
	}


	
	public boolean getHasRead() {
		return hasRead;
	}

	public void setHasRead(boolean hasRead) {
		this.hasRead = hasRead;
	}

}
